/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "test_base.h"

#include <chrono>

uint32_t TestBase::TestPerformance(uint32_t testCount)
{
    CreateBitmapCanvas();
    auto timeZero = std::chrono::high_resolution_clock::now();
    auto start = std::chrono::high_resolution_clock::now();
    for (size_t i = 0; i < testCount; i++) {
        OnTestPerformance();
    }
    auto end = std::chrono::high_resolution_clock::now();
    return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}

void TestBase::CreateBitmapCanvas()
{
    if (bitmap_) {
        OH_Drawing_BitmapDestroy(bitmap_);
    }
    bitmap_ = OH_Drawing_BitmapCreate();
    OH_Drawing_BitmapFormat cFormat = {COLOR_FORMAT_BGRA_8888,ALPHA_FORMAT_OPAQUE};
    OH_Drawing_BitmapBuild(bitmap_, DEFAULT_WIDTH, DEFAULT_HEIGHT, &cFormat);
    
    if (canvas_) {
        OH_Drawing_CanvasDestroy(canvas_);
    }
    canvas_ = OH_Drawing_CanvasCreate();
    OH_Drawing_CanvasBind(canvas_, bitmap_);
    OH_Drawing_CanvasClear(canvas_, DEFAULT_BACKGROUND_COLOR);
}

size_t TestBase::GetUtf8CharLength(const char* utf8Char)
{
    unsigned  char c = static_cast<unsigned char>(*utf8Char);
    if ( c <= 0x7F) {
        return 1;
    } else if ((c & 0xE0) == 0xC0) {
        return 2;
    } else if ((c & 0xF0) == 0xE0) {
        return 3;
    } else if ((c & 0xF8) == 0xF0) {
        return 4;
    }
    return 0;
}